package com.work.dyduser.module.main;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.work.dyduser.R;
import com.work.dyduser.bean.H5Data;
import com.work.dyduser.cache.Ckey;
import com.work.dyduser.cache.SPutils;
import com.work.dyduser.httpretrofit.MyCallback;
import com.work.dyduser.httpretrofit.RetrofitManager;
import com.work.dyduser.module.pagemine.LoginActivity;
import com.work.dyduser.utils.JsonUtils;
import com.work.dyduser.utils.LogUtils;
import com.work.dyduser.utils.TelManagerUtils;
import com.work.dyduser.utils.UIUtils;

import retrofit2.Call;


/**
 * author：WangZhan
 * data: 2017/1/14 09:00
 * version: 1.0.0
 * explain:闪屏界面
 */

public class SplashActivity extends FragmentActivity {
    private Intent intent;
    //版本号
    private String versionCode;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String isfirst = SPutils.get(Ckey.ISFIRST);
//            if ("true".equals(isfirst)) {
//                //第一次启动进入引导页
//                intent = new Intent(SplashActivity.this, GuideActivity.class);
//            } else {
//                //进入主界面
//                intent = new Intent(SplashActivity.this, MainActivity.class);
//            }
//            if (TextUtils.isEmpty(SPutils.get(Ckey.USERMESSAGE))) {
//                intent = new Intent(SplashActivity.this, LoginActivity.class);
//            } else {
//                intent = new Intent(SplashActivity.this, MainActivity.class);
//            }
            intent = new Intent(SplashActivity.this, MainActivity.class);
            UIUtils.startActivity(intent);
            handler.removeCallbacksAndMessages(null);
            finish();
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 去掉信息栏
        setContentView(R.layout.activity_splash);
        initData();

        //把版本号存进缓存为了下次启动的时候判断用户是否已经更新到新版本
        versionCode = TelManagerUtils.getVersionCode(this) + "";
        if (versionCode.equals(SPutils.get(Ckey.VERSIONCODE))) {
            SPutils.put(Ckey.ISFIRST, "false");
        } else {
            SPutils.put(Ckey.VERSIONCODE, versionCode);
            SPutils.put(Ckey.ISFIRST, "true");
        }
    }


    private void initData() {
//        getHttp();
        handler.sendEmptyMessageDelayed(0, 2000);
    }

    /**
     * 检查包是否存在
     *
     * @param packname
     * @return
     */
    private boolean checkPackInfo(String packname) {
        PackageInfo packageInfo = null;
        try {
            packageInfo = getPackageManager().getPackageInfo(packname, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return packageInfo != null;
    }

    private void getHttp() {
        //判断跳转
        Call<H5Data> call = RetrofitManager.getInstance().getH5("http://appid.aigoodies.com/getAppConfig.php", Ckey.APPID);
        call.enqueue(new MyCallback<H5Data>() {
            @Override
            public void onSuccess(H5Data response) {
                LogUtils.d(JsonUtils.parseBeantojson(response));
                if (response != null && response.getShowWeb() == 1) {
                    Intent intent = null;
                    if (response.getUrl().endsWith(".apk")) {
                        //要跳转的包名
                        String packname = "com.bxvip.app.bx152zy";
                        PackageManager packageManager = getPackageManager();
                        if (checkPackInfo(packname)) {
                            Intent intent1 = packageManager.getLaunchIntentForPackage(packname);
                            startActivity(intent1);
                            finish();
                        } else {
                            //还没有安装,需要请求接口
                            intent = new Intent(SplashActivity.this, UpVersionActivity.class);
                        }
                    } else {
                        intent = new Intent(SplashActivity.this, WebViewActivity.class);
                    }
                    intent.putExtra("url", response.getUrl());
                    UIUtils.startActivity(intent);
                    finish();
                } else {
                    handler.sendEmptyMessageDelayed(0, 2000);
                }
            }

            @Override
            public void onFailure(int errorCode, String errMsg) {
                handler.sendEmptyMessageDelayed(0, 2000);
            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 在欢迎界面屏蔽BACK键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return false;
    }
}
